<script lang="ts" setup>
import PageTable from "@/components/PageTable/index.vue";
import SearchHeader from "@/components/SearchHeader/index.vue";
import PageContainer from "@/components/PageContainer/index.vue";
import {reactive, ref} from "vue";
import {CirclePlusFilled,Edit,Remove } from '@element-plus/icons-vue'
import MeterDialog from "@/views/maintenance/components/MeterDialog.vue";
import {ButtonColor} from "@/utils/buttonsColor";
import { ElMessageBox } from 'element-plus'
import { message } from "@/utils/message";
import { editMeter, materList } from "@/api/demoApi";

defineOptions({
  name: "meter"
});

/**-------------------搜索-------------------**/
//查询参数
const queryList = [
  {key:'projectNo',label:'电表号',type:'input'},
  {key:'projectName',label:'所属站点',type:'input'},
  {key:'projectName',label:'电表状态',type:'select',options:[
      {label:'正常',value:0},
      {label:'停用',value:1}
    ]},
]
//分页
const pageQuery = reactive({
  page:1,
  size:15
})
//查询回调
const searchHandler = (queryForm)=>{
  console.log(queryForm)
}
//分页回调
const paginationHandler = (pageInfo) =>{
  pageQuery.page = pageInfo.page
  pageQuery.size = pageInfo.size
}

/**-------------------列表-------------------**/
//总条数
const total = ref(0)
//表格列
const tableColumns = [
  {label:'电表号',key:'meterNo'},
  {label:'所属站点',key:'stationName'},
  {label:'所属项目',key:'projectName'},
  {label:'业主',key:'ownerName'},
  {label:'运营商',key:'operatorName'},
  {label:'建表日期',key:'initTime'},
  {label:'当前度数',key:'currentDegree'},
  {label:'最近抄表日期',key:'sendRecent'},
  {label:'电表状态',key:'status',custom:true},
  {label:'操作',key:'operation',custom:true,width: 250}
]
//表格行数据
const tableData = ref([])
const getTableData = ()=>{
  tableData.value = materList()
}
getTableData()

/**-------------------操作-------------------**/
//弹窗
const meterDialog = ref()
const openDialog = (type:string,row:object)=>{
  switch (type){
    case 'add':
      meterDialog.value.dialogShow('添加电表')
      break
    case 'edit':
      meterDialog.value.dialogShow('编辑电表',row)
      break
  }
}
//停用
const delHandler = async row =>{
  try{
    await ElMessageBox.confirm('确认要停用该电表吗？', '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
    )
    //todo 删除接口
    row.status = 1
    editMeter(row)
    message('电表已停用',{type:'success'})
    getTableData()
  }catch(err){

  }
}
//未完成提示
const notDone = ()=>{
  message('抱歉，功能设计中...',{type:'warning'})
}
</script>

<template>
  <PageContainer>
    <!--顶部筛选-->
    <SearchHeader :query-list="queryList" @search="searchHandler">
      <template #button>
        <el-button :color="ButtonColor.ADD" :icon="CirclePlusFilled" @click="openDialog('add',null)">添加</el-button>
      </template>
    </SearchHeader>
    <!--    表格-->
    <PageTable :columns="tableColumns" :page="pageQuery.page" :size="pageQuery.size" :table-data="tableData" :total="total" @pagination="paginationHandler">
      <template #status="{row}">
        <el-tag v-if="row.status === 0" type="primary">正常</el-tag>
        <el-tag v-if="row.status === 1" type="danger">停用</el-tag>
      </template>
      <template #operation="{row}">
        <el-button :color="ButtonColor.EDIT" :icon="Edit" plain round size="small" @click="openDialog('edit',row)">编辑</el-button>
        <el-button v-if="row.status === 0" :color="ButtonColor.DEL" :icon="Remove" plain round size="small" @click="delHandler(row)">停用</el-button>
      </template>
    </PageTable>

    <!--  弹窗-->
    <MeterDialog ref="meterDialog" @done="getTableData" />
  </PageContainer>
</template>

<style lang="scss" scoped>

</style>
